//
//  ZCLiveBuildManager.m
//  ZCLiveSDK
//
//  Created by 彭军 on 2017/11/18.
//  Copyright © 2017年 Elvis. All rights reserved.
//

#import "ZCLiveBuildManager.h"
#import "ZCPreLiveViewController.h"
#import "NLBaseNC.h"
#import "ZCLiveViewController.h"
@interface ZCLiveBuildManager()
@property (nonatomic,copy)ZCCloseCompletion closeBlock;
@end

static ZCLiveBuildManager *liveBuildManager = nil;
@implementation ZCLiveBuildManager
+ (ZCLiveBuildManager *)sharedInstance {
    static dispatch_once_t oncetoken;
    dispatch_once(&oncetoken,^{
        if (!liveBuildManager) {
            liveBuildManager = [[self alloc] init];
        }
    });
    return liveBuildManager;
}

- (id)init {
    self = [super init];
    if (self) {

        self.resolution = ZCResolution_HIGH;
    }
    return self;
}

-(void)connectWithTarget:(UIViewController *)target phoneNum:(NSString*)phoneNum cartId:(NSString *)cartId longitude:(NSNumber *)longitude latitude:(NSNumber *)latitude closeBlock:(ZCCloseCompletion)closeBlock{
    [self connectWithTarget:target phoneNum:phoneNum cartId:cartId longitude:longitude latitude:latitude accountId:nil info:nil closeBlock:closeBlock];
}

-(void)connectWithTarget:(UIViewController *)target phoneNum:(NSString*)phoneNum cartId:(NSString *)cartId longitude:(NSNumber *)longitude latitude:(NSNumber *)latitude accountId:(NSString *)accountId info:(NSString *)info closeBlock:(ZCCloseCompletion)closeBlock{
    
    self.closeBlock = closeBlock;
    if (self.accountId.length) {
        _accountId = accountId;
    }
    _info = info;
    _latitude = latitude;
    _longitude = longitude;
    
    if (target && [target isKindOfClass:[UIViewController class]] && phoneNum.length && cartId.length && longitude && latitude) {
        
        if (phoneNum.length && cartId.length) {
            
            ZCLiveViewController *liveVC = [[ZCLiveViewController alloc] init];
            
            //            NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"ZCLiveSDKBundle" withExtension:@"bundle"]];
            //            ZCPreLiveViewController *preLiveVC = [[ZCPreLiveViewController alloc] initWithNibName:@"ZCPreLiveViewController" bundle:bundle];
            liveVC.cartId = cartId;
            liveVC.phoneNum = phoneNum;
            __weak typeof(self) weakSelf = self;
            liveVC.closeBlock = ^(BOOL success, NSString *info) {
                if (weakSelf.closeBlock) {
                    weakSelf.closeBlock(nil);
                }
            };
            NLBaseNC *nav = [[NLBaseNC alloc] initWithRootViewController:liveVC];
            [target presentViewController:nav animated:YES completion:^{
                
            }];
        }
    }else{
        
        self.closeBlock(@"target/phoneNum/cartId/longitude/latitude 五个参数不能为空");
    }
    
}
@end

